﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.Threading;
using System.Net;
using System.Runtime.InteropServices;

namespace DomainTool
{
    public partial class Form1 : Form
    {
        public string iniFile = "c:\\DOMAIN.INI";
        public List<String> length4NumDomains;
        public List<String> length3Domains;
        public List<String> length4CharDomains;
        public List<String> pinyin2Domains ;
        public List<String> pinyin3Domains;
        public List<String> startWith51Domains ;
        public List<String> startWithChinaDomains;
        string suffix = "";
        string dropListUrl = "http://www.name.com/deleting-domains/index.php?tld=com&per_page_count=1000&drop_date={dropDate}&length=15&yahoo_totalhits=&yahoo_totalhits_pagedata=&noDash=on&data_analyzer_1=&data_analyzer_2=&"
               + "data_analyzer_3=&data_analyzer_4=&data_analyzer_5=&data_analyzer_6=&losing_registrar=ALL&searchType=contains-all-of&searchTerm=&submitted=1&submit=+Search+&orderMethod=&orderBy=&per_page=";
        public string dateStr;
        //name 分页正则
        string domainCountReg = @"Displaying domains <b>\d+ - \d+</b> out of  <b>(\d+)</b>";
        //name 域名列表正则
        string nameComDomainReg = @"<td class='' nowrap>([a-zA-Z0-9]+([a-zA-Z0-9\-\.]+)?\.(com|net|org))</td>";
        List<string> domainNameList = new List<string>();
        int currentPage = 0;
        int pageCount = 1;
        //开始页数
        int startPage = 1;
        Thread thread;
        Thread thread1;
        //备份
        FileStream tSave;
        StreamWriter tsw;
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        FileStream loggerFile;
        StreamWriter logger;
        public Form1()
        {
            InitializeComponent();
            loggerFile = new FileStream("c:/Debug.log", FileMode.Append);
            //实例化一个StreamWriter-->与fs相关联
            logger = new StreamWriter(loggerFile);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
           
            openFileDialog1.RestoreDirectory =true;
            DialogResult dr=this.openFileDialog1.ShowDialog();
            if (dr.ToString().Equals("OK"))
            {
                this.txtSourcePath.Text = openFileDialog1.FileName;
            }
        }
       
        public void domainDownload()
        {
            
            //需要同步的对象currentPage pageCount
            Regex regAddr = new Regex(nameComDomainReg);
            Regex regex = new Regex(domainCountReg);
            //取文章地址
            string url;
            while(true)
            {
                lock (this)
                {
                    if (currentPage > pageCount)
                    {
                        break;
                    }
                    
                //获取地址
                url = dropListUrl + ((currentPage - 1) * 1000 + 1);
                 
                currentPage++;
                }
                url=url.Replace("{dropDate}", this.dateStr);
                logger.WriteLine(System.DateTime.Now.ToString() + "  开始下载:" + url);
                logger.Flush();
                string contents = HttpUtils.httpGet(url);
                //更新总量
                Match result = regex.Match(contents);
                if (result.Length > 0)
                {
                    pageCount = Int32.Parse(result.Groups[1].Value) / 1000;
                    if (Int32.Parse(result.Groups[1].Value) % 1000 > 0)
                        pageCount += 1;
                }
                MatchCollection results = regAddr.Matches(contents);
                foreach (Match match in results)
                {
                    domainNameList.Add(match.Groups[1].Value);
                    tsw.WriteLine(match.Groups[1].Value);
                }
                tsw.Flush();
                WritePrivateProfileString("", "LAST", currentPage.ToString(), iniFile);
                WritePrivateProfileString("", "DATE", this.dateStr, iniFile);
                WritePrivateProfileString("", "TOTAL", this.pageCount.ToString(), iniFile);
               
            }
        }
        
        private void btnFilter_Click(object sender, EventArgs e)
        {
            dateStr = this.dateTimePicker1.Value.ToString("yyyy-MM-dd");
            toolStripProgressBar1.Value = 0;
            logger.WriteLine(System.DateTime.Now.ToString() + "  筛选开始");
            
            if (this.txtSavePath.Text == "")
            {
                MessageBox.Show("请选择域名目标文件!");
                return;
            }
          
            if(chkCom.Checked)
            {
                suffix+="com";
            }
             if(chkNet.Checked)
            {
                suffix+="|net";
            }
             if(chkOrg.Checked)
            {
               suffix+="|org";
            }
            if(suffix.StartsWith("|"))
            {
                suffix.Substring(1);
            }
            List<string> domains;
            if (this.txtSourcePath.Text == "")
            {
                tsslStatusText.Text = "从网站下载";
                tSave = new FileStream("c:/备份/" + this.dateStr+".txt", FileMode.Append);
                //实例化一个StreamWriter-->与fs相关联
                tsw = new StreamWriter(tSave);
                //获取详细信息
                string blogIndexTxt;
                string initUrl = "http://www.name.com/deleting-domains/index.php?tld=com&per_page_count=50&drop_date={dropDate}&length=15&yahoo_totalhits=&yahoo_totalhits_pagedata=&noDash=on&data_analyzer_1=&data_analyzer_2=&"
                + "data_analyzer_3=&data_analyzer_4=&data_analyzer_5=&data_analyzer_6=&losing_registrar=ALL&searchType=contains-all-of&searchTerm=&submitted=1&submit=+Search+&orderMethod=&orderBy=&per_page=";
                string tempUrl = initUrl.Replace("{dropDate}", this.dateStr);
                //startPage==上次pagecount
                StringBuilder temp = new StringBuilder(80);
                GetPrivateProfileString("", "LAST", "0", temp, 80, iniFile);
                if (temp.ToString().Equals(""))
                {
                    startPage = 1;
                }
                else
                { startPage = int.Parse(temp.ToString()); }

                GetPrivateProfileString("", "TOTAL", "0", temp, 80, iniFile);
                int total = 1;
                if (!temp.ToString().Equals(""))
                {
                    total = int.Parse(temp.ToString());
                }


                GetPrivateProfileString("", "DATE", "", temp, 80, iniFile);

                if (!temp.ToString().Equals(this.dateStr))
                {
                    total = 0;
                    startPage = 1;
                }
                logger.WriteLine(System.DateTime.Now.ToString() + " PAGECOUNT=" + startPage);
                logger.WriteLine(System.DateTime.Now.ToString() + " total=" + total);
                logger.WriteLine(System.DateTime.Now.ToString() + "  开始下载:" + dropListUrl + startPage);
                logger.Flush();
                blogIndexTxt = HttpUtils.httpGet(tempUrl + startPage);
                this.currentPage = startPage;
                Regex regex = new Regex(domainCountReg);
                Match result = regex.Match(blogIndexTxt);
                if (result.Length > 0)
                {

                    pageCount = Int32.Parse(result.Groups[1].Value) / 1000;
                    if (Int32.Parse(result.Groups[1].Value) % 1000 > 0)
                    {
                        pageCount += 1;
                        if (total != 0 && int.Parse(result.Groups[1].Value) == total)
                        {
                            MessageBox.Show(("站点没有域名啦!"));
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show(("站点没更新" + this.dateStr + "域名呢!"));
                    return;
                }
                //Thread threadDetails = new Thread(new ThreadStart(getDetailsFromName));
                //threadDetails.Start();
                //threadDetails.Join();
                //创建2个线程，下载
                 thread = new Thread(new ThreadStart(domainDownload));
                thread.Start();
                 thread1 = new Thread(new ThreadStart(domainDownload));
                thread1.Start();
                
                //等待线程结束
                while (thread1.IsAlive)
                {
                    continue;
                }
                //thread.Join();
                //thread1.Join();
                tsw.Flush();
                tsw.Close();
                tSave.Close();
                //备份完毕
                logger.WriteLine(System.DateTime.Now.ToString() + "  下载结束: pagecount=" + this.pageCount);
                logger.Flush();
                //保存pageCOUNT 下次用
                WritePrivateProfileString("", "LAST", currentPage.ToString(), iniFile);
                WritePrivateProfileString("", "TOTAL", this.pageCount.ToString(), iniFile);
                WritePrivateProfileString("", "DATE", this.dateStr, iniFile);
                domains = this.domainNameList;
                tsslStatusText.Text = "域名查找完毕，进行筛选!";
            }
            else
            {
                tsslStatusText.Text = "从文件读取";
                domains = FindDomainUtils.findDomainsFromFile(this.txtSourcePath.Text, suffix);
            }
            length3Domains = FindDomainUtils.length3Domain(domains, this.suffix);
             //4数字
             if (chk4Num.Checked)
             {
                 length4NumDomains = FindDomainUtils.length4Num(domains, this.suffix);
             }
             //4字母
             if (chk4Char.Checked)
             {
                 length4CharDomains = FindDomainUtils.length4Char(domains, this.suffix);
             }
             //2拼音
             if (chk2Pin.Checked)
             {
                 pinyin2Domains = FindDomainUtils.pinyin(domains, this.suffix, 2);
                 //去重复
                 foreach (string d in pinyin2Domains)
                 {
                     domains.Remove(d);
                 }
             }
             //3拼音
             if (chk3Pin.Checked)
             {
                 pinyin3Domains = FindDomainUtils.pinyin(domains, this.suffix, 3);
             }
             //china开头
             if (chkStartWithChina.Checked)
             {
                 this.startWithChinaDomains = FindDomainUtils.startWithChina(domains, this.suffix);
             }
             //51开头
             if (chkStartWith51.Checked)
             {
                 startWith51Domains = FindDomainUtils.startWith51(domains, this.suffix);
             }
             toolStripProgressBar1.Value = 5;
			 Thread  thread2 = new Thread(new ThreadStart(saveDomain));
             thread2.Start();
             tsslStatusText.Text = "域名筛选结束!";
             logger.WriteLine(System.DateTime.Now.ToString() + "  筛选结束");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.folderBrowserDialog1.ShowDialog();
            if (dr.ToString().Equals("OK"))
            {
                this.txtSavePath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dateTimePicker1.Value = System.DateTime.Now.AddDays(4);
            //this.txtSavePath.Text=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            StringBuilder temp = new StringBuilder(80);
            GetPrivateProfileString("","LAST", "", temp, 80, "c:\\DOMAIN.INI");
            this.label6.Text = "上次更新页数" + temp.ToString();
            
            GetPrivateProfileString("", "DIR", "", temp, 80, "c:\\DOMAIN.INI");
            this.txtSavePath.Text = temp.ToString();

        }
        //获取百度建议
        private string getRecommended(string word)
        {
            Regex yijianReg = new Regex("<span  class=\"jc\">[\\s\\S]*</span></strong><br></p>");
            string recommended = "";
            Regex linkReg = new Regex(@"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>[\s\S]*?)</a>");
            string content = HttpUtils.httpGet("http://www.baidu.com/s?wd=" + word);
            if (yijianReg.IsMatch(content))
            {
                string yijianStr = yijianReg.Match(content).ToString();
                if (linkReg.IsMatch(yijianStr))
                {
                    foreach (Match m in linkReg.Matches(yijianStr))
                    {
                        recommended += m.Groups[3].ToString() + ",";
                    }
                    if (recommended.EndsWith(","))
                    {
                        recommended.Substring(0, recommended.Length - 1);
                    }
                }
            }
            return recommended;
        }

        /**
         *把保存到文件
         * 
         **/
        private void saveDomain() 
        {
            string filePath = txtSavePath.Text + "/NAME" + this.dateStr + ".txt";
            // 写文件
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            FileStream fs;
            if (this.txtSourcePath.Text == "")
            { 
                //append
                fs = new FileStream(filePath, FileMode.Append);
            }else
            {
                fs= new FileStream(filePath, FileMode.OpenOrCreate);
            }
             
            //实例化一个StreamWriter-->与fs相关联
            StreamWriter sw = new StreamWriter(fs);

            if (length3Domains.Count > 0)
            {
                sw.WriteLine("==============================3位域名" + length3Domains.Count + "个===============================");
                foreach (string domain in length3Domains)
                {
                    sw.WriteLine(domain);
                }
            }
            //开始写入
            if (chk4Num.Checked && length4NumDomains.Count > 0)
            {
                sw.WriteLine("==============================4位数字域名" + length4NumDomains.Count + "个===============================");
                foreach (string domain in length4NumDomains)
                {
                    sw.WriteLine(domain);
                }
            }
            toolStripProgressBar1.Value = 10;
            //sw.Flush();
            //4字母
            if (chk4Char.Checked && length4CharDomains.Count > 0)
            {

                sw.WriteLine("==============================4字母域名" + length4CharDomains.Count + "个===============================");
                foreach (string domain in length4CharDomains)
                {
                    sw.WriteLine(domain);
                }
            }
            toolStripProgressBar1.Value = 15;
            //sw.Flush();
            
            //2拼音
            if (chk2Pin.Checked && pinyin2Domains.Count > 0)
            {
                sw.WriteLine("==============================2拼音域名" + pinyin2Domains.Count + "个===============================");

                foreach (string domain in pinyin2Domains)
                {
                   string recommended =getRecommended(domain.Substring(0, domain.IndexOf('.')));
                    sw.WriteLine(domain + "  建议：" + recommended);
                }
            }
            sw.Flush();
            toolStripProgressBar1.Value = 60;
            //3拼音
            if (chk3Pin.Checked && pinyin3Domains.Count > 0)
            {
                sw.WriteLine("==============================3拼音域名" + pinyin3Domains.Count + "个===============================");
                foreach (string domain in pinyin3Domains)
                {
                    string recommended = getRecommended(domain.Substring(0, domain.IndexOf('.')));
                    sw.WriteLine(domain + "  建议：" + recommended);                   
                }
            }
            sw.Flush();
            //china开头
            toolStripProgressBar1.Value = 80;
            if (this.chkStartWithChina.Checked && startWithChinaDomains.Count > 0)
            {
                sw.WriteLine("==============================china开头域名" + startWithChinaDomains.Count + "个===============================");
                foreach (string domain in this.startWithChinaDomains)
                {
                    sw.WriteLine(domain);
                }
            }
            toolStripProgressBar1.Value = 90;
            //51开头
            if (chk4Num.Checked && startWith51Domains.Count > 0)
            {
                sw.WriteLine("==============================51开头域名" + startWith51Domains.Count + "个===============================");
                foreach (string domain in this.startWith51Domains)
                {
                    sw.WriteLine(domain);
                }
            }
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
            toolStripProgressBar1.Value = 100;
            toolStripStatusLabel1.Text = "查找域名结束";
            MessageBox.Show("筛选成功!");
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            logger.Flush();
            logger.Close();
            loggerFile.Close();
            //异常退出
            currentPage=currentPage>0?currentPage:0;
          //  WritePrivateProfileString("", "LAST", currentPage.ToString(), "c:\\DOMAIN.INI");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(thread!=null){
               // thread.Suspend();
               // thread1.Suspend();
            }
           
        }

        private void txtSavePath_TextChanged(object sender, EventArgs e)
        {
            WritePrivateProfileString("", "DIR", this.txtSavePath.Text, "c:\\DOMAIN.INI");
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            WritePrivateProfileString("", "LAST","", iniFile);
            WritePrivateProfileString("", "TOTAL", "", iniFile);
            WritePrivateProfileString("", "DATE", "", iniFile);
            this.label6.Text = "";
        }
        private void getDetailsFromName()
        {
           
        }
    }
} 
